package com.payment.system.domain.wxpay;

import java.io.UnsupportedEncodingException;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;



/**
 * 统一订单请求
 * 
 * @author juxing
 * 
 */
@XmlRootElement(name = "xml")
public class WxUnifiedOrderReq {


	public WxUnifiedOrderReq() {

	}

	/**
	 * 统一下单建议初始化方法
	 * 
	 * @param body
	 *            商品描述
	 * @param outTradeNo
	 *            商户订单号
	 * @param totalFee
	 *            总金额
	 * @param spbillCreateIp
	 *            终端IP
	 * @throws UnsupportedEncodingException 
	 */
	public WxUnifiedOrderReq(String body, String outTradeNo, Integer totalFee, String spbillCreateIp) throws UnsupportedEncodingException {
		this.appId = WxAppPayConfig.instance().appId;
		this.mchId = WxAppPayConfig.instance().mchId;
		this.nonceStr = UUID.randomUUID().toString().trim().replaceAll(" ", "").replace("-", "");
		this.body = body;
		this.outTradeNo = outTradeNo;
		this.totalFee = totalFee;
		this.spbillCreateIp = spbillCreateIp;
		this.notifyUrl = WxAppPayConfig.instance().notifyUrl;
		this.tradeType = WxAppPayConfig.instance().tradeType;
		this.sign = createSign();
	}

	/**
	 * 签名
	 * 
	 * @return
	 */
	private String createSign() {
		SortedMap<String, Object> m = new TreeMap<>();
		m.put("appid", this.appId);
		m.put("mch_id", this.mchId);
		m.put("nonce_str", this.nonceStr);
		m.put("body", this.body);
		m.put("out_trade_no", this.outTradeNo);
		m.put("total_fee", this.totalFee);
		m.put("spbill_create_ip", this.spbillCreateIp);
		m.put("notify_url", this.notifyUrl);
		m.put("trade_type", this.tradeType);
		return WxPayUtil.sign(m);
	}

	/** 应用ID */
	private String appId;
	/** 商户号 */
	private String mchId;
	/** 随机字符串 */
	private String nonceStr;
	/** 签名 */
	private String sign;
	/** 商品描述 */
	private String body;
	/** 商户订单号 */
	private String outTradeNo;
	/** 总金额 */
	private Integer totalFee;
	/** 终端IP */
	private String spbillCreateIp;
	/** 通知地址 */
	private String notifyUrl;
	/** 交易类型 */
	private String tradeType;

	public static String[] CDATA_TAG = { "appid", "mch_id", "nonce_str", "sign", "body", "out_trade_no", "total_fee", "spbill_create_ip", "total_fee" };

	@XmlElement(name = "appid")
	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	@XmlElement(name = "mch_id")
	public String getMchId() {
		return mchId;
	}

	public void setMchId(String mchId) {
		this.mchId = mchId;
	}

	@XmlElement(name = "nonce_str")
	public String getNonceStr() {
		return nonceStr;
	}

	public void setNonceStr(String nonceStr) {
		this.nonceStr = nonceStr;
	}

	@XmlElement(name = "sign")
	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}

	@XmlElement(name = "body")
	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	@XmlElement(name = "out_trade_no")
	public String getOutTradeNo() {
		return outTradeNo;
	}

	public void setOutTradeNo(String outTradeNo) {
		this.outTradeNo = outTradeNo;
	}

	@XmlElement(name = "total_fee")
	public Integer getTotalFee() {
		return totalFee;
	}

	public void setTotalFee(Integer totalFee) {
		this.totalFee = totalFee;
	}

	@XmlElement(name = "spbill_create_ip")
	public String getSpbillCreateIp() {
		return spbillCreateIp;
	}

	public void setSpbillCreateIp(String spbillCreateIp) {
		this.spbillCreateIp = spbillCreateIp;
	}

	@XmlElement(name = "notify_url")
	public String getNotifyUrl() {
		return notifyUrl;
	}

	public void setNotifyUrl(String notifyUrl) {
		this.notifyUrl = notifyUrl;
	}

	@XmlElement(name = "trade_type")
	public String getTradeType() {
		return tradeType;
	}

	public void setTradeType(String tradeType) {
		this.tradeType = tradeType;
	}

}
